//chip  芯片服务
// 256尺寸最大189.597kb 
export default {
    //字符串转图片base64
    str2img(str) {
        var uint8array = (new TextEncoder('utf-8')).encode(str);
        var dataSize = Math.ceil(Math.sqrt(uint8array.length / 3));
        var paddedData = new Uint8ClampedArray(dataSize * dataSize * 4);
        var idx = 0;
        for (var i = 0; i < uint8array.length; i += 3) {
            var subArray = uint8array.subarray(i, i + 3);
            paddedData.set(subArray, idx);
            paddedData.set([255], idx + 3);
            idx += 4;
        }
        var imageData = new ImageData(paddedData, dataSize, dataSize);
        var imgSize = 256;
        var canvas = document.createElement('canvas');
        canvas.width = canvas.height = imgSize;
        var ctx = canvas.getContext('2d');
        ctx.fillStyle = '＃AA0000';
        ctx.fillRect(0, 0, imgSize, imgSize);
        ctx.fillStyle = 'rgb(' + dataSize + ',0,0)';
        ctx.fillRect(0, 0, 1, 1);
        ctx.putImageData(imageData, 0, 1);

        return canvas.toDataURL();
    },

    //图片转string
    img2str(imgUrl, callback) {
        if (!imgUrl) {
            console.log('未设置图片url');
            return
        }
        var img = new Image();
        img.onload = function () {
            var imgSize = img.width;
            var canvas = document.createElement('canvas');
            canvas.width = canvas.height = imgSize;
            var ctx = canvas.getContext('2d');
            ctx.drawImage(img, 0, 0);
            var headerData = ctx.getImageData(0, 0, 1, 1);
            var dataSize = headerData.data[0];

            var imageData = ctx.getImageData(0, 1, dataSize, dataSize);
            var paddedData = imageData.data;
            var uint8array = new Uint8Array(paddedData.length / 4 * 3);
            var idx = 0;
            for (var i = 0; i < paddedData.length - 1; i += 4) {
                var subArray = paddedData.subarray(i, i + 3);
                uint8array.set(subArray, idx);
                idx += 3;
            }
            var includeBytes = uint8array.length;
            for (var i = uint8array.length - 1; i > 0; i--) {
                if (uint8array[i] == 0) {
                    includeBytes--;
                } else {
                    break;
                }
            }
            var data = uint8array.subarray(0, includeBytes);
            var strData = (new TextDecoder('utf-8')).decode(data);

            callback && callback(strData);
        };
        img.src = imgUrl;
    },
    init(cb, dom){
        var container = dom||document.body;
        //拖拽
        container.addEventListener('dragover', (e) => {
            e.preventDefault();
            container.className.indexOf('ondrag')<0?container.className+=' ondrag':'';
        });
        container.ondrop = (e) => {
            container.className=container.className.replace(' ondrag','')
            var list = e.dataTransfer.files;
            this.reader(list[0], (data) => {
                this.loadImage(data,cb);
            });
        };
        document.ondragover = function (e) {
            e.preventDefault();
        };
        document.ondrop = function (e) {
            e.preventDefault();
        };
    },
    //加载图片
 loadImage(data,cb) {
    var bgTime = new Date()
    this.img2str(data, (str) => {
        cb && cb(str)
        var time = new Date().getTime() - bgTime.getTime()
        console.log('解密' + data.length / 1000 + 'kb到字符串,用时:' + time + 'ms')
    })
},

// 读取url文件内容
 reader(url, callback) {
    var reader = new FileReader();
    reader.readAsDataURL(url);
    reader.onload = function () {
        callback && callback(reader.result)
    }
}
}





